package com.compmov.libraries;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.compmov.datamodel.Persona;
import com.compmov.datamodel.Solicitud;
import com.compmov.socialnetwork.PerfilAmigo;
import com.compmov.socialnetwork.R;
import com.compmov.socialnetwork.Solicitudes;
import com.compmov.webservice.SolicitudWSClient;
import com.compmov.webservice.WSClientException;

public class SolicitudesAdapter extends BaseAdapter {

	private Activity activity;
	private List<Solicitud> data;

	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;

	public SolicitudesAdapter(Activity a, List<Solicitud> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		public TextView text;
		public TextView textFS;
		public ImageView image;
		public ImageButton acept;
		public ImageButton cancel;

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.item_solicitudes, null);
			holder = new ViewHolder();
			holder.text = (TextView) vi.findViewById(R.id.textN);
			;
			holder.textFS = (TextView) vi.findViewById(R.id.textFS);
			;
			holder.image = (ImageView) vi.findViewById(R.id.image);
			holder.acept = (ImageButton) vi.findViewById(R.id.acept);
			holder.cancel = (ImageButton) vi.findViewById(R.id.cancel);

			vi.setTag(holder);
		} else
			holder = (ViewHolder) vi.getTag();

		Solicitud sol = data.get(position);
		Persona per = sol.getSolicitante();

		holder.text.setText(per.getNombreCompleto());
		holder.textFS.setText("Fec. Sol.: " + sol.getFechaSolicitud());
		holder.image.setTag(per.getFotografia());
		holder.image.setId(per.getId());
		imageLoader.DisplayImage(per.getFotografia(), activity, holder.image);

		holder.image.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent nuevoIntent = new Intent(activity, PerfilAmigo.class);
				nuevoIntent.putExtra("idPer", view.getId());
				activity.startActivity(nuevoIntent);

			}
		});
		holder.cancel.setTag(position);
		holder.cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				int id = (Integer) view.getTag();

				Solicitud sol = data.get(id);
				sol.setEstado(com.compmov.libraries.Utils.CANCEL_SOLICITUD);
				obtenAceptar(sol, "cancelar");
				}

			
		});
		holder.acept.setTag(position);
		holder.acept.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				int id = (Integer) view.getTag();

				Solicitud sol = data.get(id);
				sol.setEstado(com.compmov.libraries.Utils.ACEPT_SOLICITUD);
				obtenAceptar(sol, "aceptar");

			}
		});

		return vi;
	}

	private void obtenAceptar(final Solicitud solicitud, String mensaje) {
		final AlertDialog.Builder alert = new AlertDialog.Builder(activity);
		alert.setMessage("Desea " + mensaje + " la solicitud de "
				+ solicitud.getSolicitante().getNombreCompleto() + "?");

				alert.setPositiveButton("Aceptar",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						SolicitudWSClient solicitudWS = new SolicitudWSClient(
						"solicitud");
				try {
					if (solicitudWS.actualizarSolicitud(solicitud)) {
						Intent intent = new Intent();
						intent.setClass(activity, Solicitudes.class);
						activity.startActivity(intent);
						activity.finish();
					}

				} catch (WSClientException e) {
					// TODO Auto-generated catch block
					Toast.makeText(activity, e.getMessage(),
							Toast.LENGTH_SHORT).show();
				}
					}
				});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.cancel();
					}
				});
		alert.show();

	}
}